<template>
  <el-dialog
    v-if="dialogVisible"
    width="40%"
    :title="title"
    :lock-scroll="false"
    :visible.sync="dialogVisible"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
      <el-form-item prop="categoryId" label="应用分类">
        <el-select v-model="form.categoryId" class="w_100" placeholder="请选择应用分类" clearable filterable>
          <el-option
            v-for="item in clientCategoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="logo" label="LOGO">
        <CropperImage :img-src="form.logo" @input="handleLogoInput">
          <i class="fa-brands fa-uncharted" />
        </CropperImage>
      </el-form-item>
      <el-form-item prop="name" label="应用名称">
        <el-input v-model="form.name" placeholder="请输入应用名称" />
      </el-form-item>
      <el-form-item prop="introd" label="应用介绍">
        <el-input v-model="form.introd" type="textarea" placeholder="请输入应用介绍" :autosize="{ minRows: 4, maxRows: 8}" :maxlength="500" show-word-limit />
      </el-form-item>
      <el-form-item prop="url" label="访问地址">
        <el-input v-model="form.url" placeholder="请输入访问地址" />
      </el-form-item>
      <el-form-item prop="isSso" label="统一认证">
        <el-switch v-model="form.isSso" :active-value="1" active-text="是" :inactive-value="0" inactive-text="否" />
      </el-form-item>
      <el-form-item prop="isSso" label="同步数据">
        <el-switch v-model="form.isSync" :active-value="1" active-text="是" :inactive-value="0" inactive-text="否" />
      </el-form-item>
      <el-form-item prop="status" label="应用状态">
        <el-switch v-model="form.status" :active-value="1" active-text="启用" :inactive-value="0" inactive-text="禁用" />
      </el-form-item>
      <el-form-item prop="sorted" label="排序">
        <el-input-number v-model="form.sorted" class="w_100" :min="1" :max="9999" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add, edit, getInfo } from '@/api/client/'
import { getAll } from '@/api/client/clientCategory'
import { getProxyUrl } from '@/utils/proxy'

export default {
  name: 'SaveOrEditVue',
  components: {
    CropperImage: () => import('@/components/CropperImage/index.vue')
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'add'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      title: '新增',
      clientCategoryList: [],
      form: {
        logo: '',
        name: '',
        isSso: 0,
        status: 1,
        sorted: 1
      },
      rules: {
        categoryId: [{ required: true, message: '请选择应用分类', trigger: 'blur' }],
        logo: [{ required: true, message: '请上传应用LOGO', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        url: [{ required: true, message: '请输入应用访问地址', trigger: 'blur' }],
        secretKey: [{ required: true, message: '请输入API接口调用秘钥', trigger: 'blur' }],
        isSso: [{ required: true, message: '请选择是否集成统一认证', trigger: 'blur' }],
        status: [{ required: true, message: '请选择应用状态', trigger: 'blur' }],
        sorted: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getClientCategoryList()
    switch (this.type) {
      case 'add':
        this.title = '新增'
        break
      case 'edit':
        this.title = '编辑'
        getInfo(this.id).then(res => {
          this.form = res.data
        })
        break
      default:
        break
    }
  },
  methods: {
    getProxyUrl,
    getClientCategoryList() {
      getAll().then((res) => {
        this.clientCategoryList = res.data
      })
    },
    handleLogoInput(value) {
      this.form.logo = value
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          switch (this.type) {
            case 'add':
              add(this.form).then(res => {
                this.$message.success('添加成功')
                this.handleGetList()
                this.handleClose()
              })
              break
            case 'edit':
              edit(this.form).then(res => {
                this.$message.success('编辑成功')
                this.handleGetList()
                this.handleClose()
              })
              break
            default:
              break
          }
        }
      })
    },
    handleGetList() {
      this.$emit('getList')
    },
    handleClose() {
      this.$emit('handleClose')
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep.logo-uploader .el-upload {
  $--logo-width: 80px;
  $--logo-height: 80px;
  width: $--logo-width;
  height: $--logo-height;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

  &:hover{
    border-color: #409EFF;
  }

  .logo-uploader-icon {
    font-size: 18px;
    color: #c0c0c0;
    width: $--logo-width;
    height: $--logo-height;
    line-height: $--logo-height;
    text-align: center;
  }

  .logo {
    width: $--logo-width;
    height: $--logo-height;
    display: block;
  }
}
</style>
